﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.IO;
using Valentia.Kinection.DataTypes;
using Valentia.Kinection.Remoting.Helper;

namespace Valentia.Kinection.Remoting.ServerTest
{
    public partial class Form1 : Form
    {
        #region Declarations

        private Server remotingServer = new Server(15000, "serverExample.Rem");

        private const String BREAK_LINE = "\r\n";

        #endregion

        #region Constructor

        public Form1()
        {
            InitializeComponent();
        }

        #endregion

        #region Form Control Events

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                remotingServer.Start();

                remotingServer.CommandFromClientReceived
                    += new Server.CommandFromClientReceivedDlg(remotingServer_CommandFromClient);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                remotingServer.Stop();

                remotingServer.CommandFromClientReceived
                   -= new Server.CommandFromClientReceivedDlg(remotingServer_CommandFromClient);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSendDeviceConnectionInfo_Click(object sender, EventArgs e)
        {
            try
            {
                string[] astrDeviceInfo = txtDevicesInfo.Text.Trim().Split(new char[] { ',' });

                List<KinectDeviceInfo> lstDeviceConnectionInfo = new List<KinectDeviceInfo>();

                lstDeviceConnectionInfo.Add(new KinectDeviceInfo(astrDeviceInfo[0], astrDeviceInfo[1], KinectDeviceStatus.Connected, true));
                remotingServer.RaiseDeviceConnectivityInfoEventOnClient(lstDeviceConnectionInfo, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string[] astrClientInfo = txtClientsInfo.Text.Trim().Split(new char[] { ',' });

                List<ServiceClientInfo> lstClientConnectionInfo = new List<ServiceClientInfo>();

                lstClientConnectionInfo.Add(new ServiceClientInfo(ServiceClientType.RTCServer, astrClientInfo[0], astrClientInfo[1], true));

                remotingServer.RaiseClientConnectivityInfoEventOnClient(lstClientConnectionInfo, null);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSendConnectivityStatus_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtConnectsInfo.Text.Trim().Equals("1"))
                {
                    remotingServer.RaiseConnectionStatusInfoEventOnClient(ConnectivityStatus.BothConnected, 1, 1, String.Empty);
                }
                if (txtConnectsInfo.Text.Trim().Equals("2"))
                {
                    remotingServer.RaiseConnectionStatusInfoEventOnClient(ConnectivityStatus.BothDisconnected, 0, 0, String.Empty);
                }
                if (txtConnectsInfo.Text.Trim().Equals("3"))
                {
                    remotingServer.RaiseConnectionStatusInfoEventOnClient(ConnectivityStatus.OnlyDeviceConnected, 1, 0, String.Empty);
                }
                if (txtConnectsInfo.Text.Trim().Equals("4"))
                {
                    remotingServer.RaiseConnectionStatusInfoEventOnClient(ConnectivityStatus.OnlyServiceClientConnected, 0, 1, String.Empty);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        void remotingServer_CommandFromClient(ClientCommands command, String loginName, String password)
        {
            try
            {
                MethodInvoker UpdateSubItem = delegate
                {
                    txtActivity.Text += DateTime.Now.ToString("s")
                     + " >> remotingServer_CommandFromClient() Invoked! Command >> "
                     + command.ToString() + BREAK_LINE;
                };
                this.Invoke(UpdateSubItem);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSendNotification_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtConnectsInfo.Text.Trim().Equals("1"))
                {
                    remotingServer.RaiseNotificationEventOnClient(
                    new NotificationReceivedArgs(NotificationType.DataExtractorStarted
                        , txtNotification.Text, new Exception()));
                }
                if (txtConnectsInfo.Text.Trim().Equals("2"))
                {
                    remotingServer.RaiseNotificationEventOnClient(
                     new NotificationReceivedArgs(NotificationType.Exception
                         , txtNotification.Text, new Exception()));
                }
                if (txtConnectsInfo.Text.Trim().Equals("3"))
                {
                    remotingServer.RaiseNotificationEventOnClient(
                     new NotificationReceivedArgs(NotificationType.Kinect_Already_In_Use
                         , txtNotification.Text, new Exception()));
                }
                if (txtConnectsInfo.Text.Trim().Equals("4"))
                {
                    //remotingServer.RaiseNotificationEventOnClient(
                    //new NotificationReceivedArgs(NotificationType.SkeletonTracked
                    //    , txtNotification.Text), new Exception());
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
